const http=require('http');
const path=require('path');
const fs=require('fs');

http.createServer((req,res)=>{
    // console.log(req.url);
    var urlStr=req.url;
    var filePath=path.join(__dirname,'/text.html');
    var imgPath=path.join(__dirname,'/icon.png');
    // 当请求的资源路径是/时，响应网页文件到浏览器
    if(urlStr=='/'){
        var fileContent=fs.readFileSync(filePath);
        res.writeHead(200,{'Content-Type':'text/html;charset=utf8'});
        res.write(fileContent);
        res.end();
    }
    // else if(urlStr=='/icon.png'){
    //     var imgContent=fs.readFileSync(imgPath);
    //     res.writeHead(200,{'Content-Type':'image/png'});
    //     res.write(imgContent);
    //     res.end();
    // }
    else if(urlStr=='/getdata'){
        var arr =[
            {userName:'zhangsan',age:20},
            {userName:'lisi',age:23}
        ];
        var jsonStr=JSON.stringify(arr);//对象变成json字符串
        res.writeHead(200,{'Content-Type':'text/plain'});
        res.write(jsonStr);
        res.end();
    }
}).listen(8083);
console.log('server is listening 8083')